<template>
  <h2>吾圈一键拉群工具</h2>
  <el-row :gutter="20">
    <el-col :span="6">
      <div class="grid-content ep-bg-purple" />
    </el-col>
    <el-col :span="12">
      <div class="grid-content ep-bg-purple" />
      <el-form label-width="auto" label-position="Right" size="large" v-if="!showcz">
        <el-form-item label="账号">
          <el-input v-model="userName" />
        </el-form-item>
        <el-form-item label="密码">
          <el-input v-model="password" type="password" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSubmit" style="width: 100%;">登录</el-button>
        </el-form-item>
      </el-form>
    </el-col>
    <el-col :span="6">
      <div class="grid-content ep-bg-purple" />
    </el-col>
  </el-row>
  <el-tabs tab-position="top" style="max-height: 600px" class="demo-tabs" type="border-card" v-if="showcz">
    <el-tab-pane label="本账号好友">
      <el-form :inline="true"  class="demo-form-inline">
        <el-form-item>
          <el-button type="success" @click="getF">更新好友列表</el-button>
        </el-form-item>
        <el-form-item label="群号">
          <el-input v-model="qunhao" />
        </el-form-item>
        <el-form-item label="屏蔽用户">
          <el-input v-model="yonghuming"   placeholder="含有词名称跳过邀请"/>
        </el-form-item>
        <el-form-item >
          <el-button type="success" @click="addgr">一键拉群</el-button>
        </el-form-item>
      </el-form>
      <el-table :data="Friends" ref="singleTableRef2" id="singleTableRef" max-height="500"  border>
          <el-table-column property="userProfile.name" label="好友名称" />
          <el-table-column property="accountId" label="好友id" />
          <el-table-column property="status" label="拉群状态" />
      </el-table>
    </el-tab-pane>
  </el-tabs>
</template>
<script>
import axios from 'axios'
import { ElMessage } from 'element-plus'
import NIM from 'nim-web-sdk-ng'
import Crypto from 'crypto-js';
export default {
  name: 'App',
  data() {
    return {
      sizeForm: [],
      userName: '2632809508@qq.com',
      password: 'xx123456',
      xiazhutuo:'',
      kakaka:'',
      accid: '',
      token:'',
      accessToken:'',
      chat:null,
      groupList:[],
      xuanGroupList:[],
      showcz:false,
      yanzhengmsg:'',
      getGrouprenList:[],
      xuanzhongGrouprenlist:[],
      key:Crypto.enc.Utf8.parse("999999\0\0\0\0\0\0\0\0\0\0"),
      disabled:true,
      disabled2:true,
      qunhao:'',
      yonghuming:'',
      value1:'',
      disabled3:true,
      disabled6:true,
      loading:false,
      loading2:false,
      Friends:[],
      xuanFriends:[],
      dialogTableVisible:false,
      dialogTableVisible2:false,
      textarea:'',
      disabled4:false,
      disabled5:true,
      file:null,
      drFriends:[],
      drxuan:[],
      userxiazhu:[],
      zhangdan:[],
      userch:[],
      xzuser:[],
      idb:null,
      dbName :'my-database',
      lsxiazhu:[],
      lsch:[],
      xiazhu1:[],
      heimingdan:'',
      lsxiazhuid:0,
      lschid:0,
      jiankongqun:[],
      chuser:[],
      search:'',
      dialogTableVisible12:false,
      dialogTableVisible10:false,
    }
  },
  components: {
    
  },
  updated(){

  },
  computed:{
  },
  created(){
  },
  watch: {
  },
  mounted() {
  },
  methods: {
    addgr(){
      if(!this.qunhao){
        ElMessage({
          message: '请输入群号',
          type: 'error',
        })
        return false
      }
      let tid = this.qunhao
      if(!this.yonghuming){
        // 全部要
        this.Friends.forEach(list=>{
          let data = {
            memberList:[list.accountId],
            tid:tid,
            token:this.accessToken
          }
          axios.post('https://api.ts28.com.cn/addgr', data).then(res => {
            list.status = res.data.msg
          })
        })
      }else{
        // 不为空
        // 跳过
        this.Friends.forEach(list=>{
          if(!list.profile.nick.includes(this.yonghuming)){
            let data = {
              memberList:[list.userID],
              tid:tid,
              token:this.accessToken
            }
            axios.post('https://api.ts28.com.cn/addgr', data).then(res => {
              list.status = res.data.msg
            })
          }else{
            list.status='跳过'
          }
        })
      }

    },
    getF(){
      this.Friends=[]
      let promise = this.chat.V2NIMFriendService.getFriendList();
      promise.then(data=>{
        this.Friends=data
        console.log(this.Friends)
      })

    },
    textPa(bastr){
      let srcs = bastr;
        let decrypt = Crypto.AES.decrypt(srcs, this.key, {
            mode: Crypto.mode.ECB,
            padding: Crypto.pad.Pkcs7
        });
        return decrypt.toString(Crypto.enc.Utf8)
    },
    textjm(str){
      let srcs = str;
        let decrypt = Crypto.AES.encrypt(srcs,this.key, {
            mode: Crypto.mode.ECB,
            padding: Crypto.pad.Pkcs7
        });
        return decrypt.toString()
    },
    onSubmit() {
      // 登录
        let data = {
          password: this.password,
          serverId: '',
          userName: this.userName
        }
        let login = axios.create({
          baseURL: 'https://api.ts28.com.cn/',
          timeout: 5000, // 请求超时时间
        })
        login.post('/lhlogin', data).then(res => {
          if (res.data.code == 200) {
            //登录成功
            ElMessage({
              message: res.data.msg,
              type: 'success',
            })
            this.accid = res.data.data.accid
            this.toekn = res.data.data.token
            this.accessToken =  res.data.data.access_token
            // 登录腾讯im
            this.chat = NIM.getInstance(
                /**
                 * param1: NIMInitializeOptions
                 */
              {
                appkey: "c1ee6a2c62e80c4eb658277d0ef5864c",
                debugLevel: "debug",
                apiVersion: "v2",
              }
            )
            this.chat.V2NIMLoginService.on('onConnectStatus',(status)=> {
                //登录
                ElMessage({
                  message: status,
                  type: 'success',
                })
                this.showcz = true
                this.chat.V2NIMMessageService.on('onReceiveMessages',messages=>{
                  messages.forEach(list=>{
                    console.log(list)
                    if(list.messageType==0){
                      let msg = this.textPa(list.text)
                      if(msg == '123'){
                        let resmsg = this.textjm("hello")
                        try {
                        // 创建一条文本消息
                        const message = this.chat.V2NIMMessageCreator.createTextMessage(resmsg)
                        // 发送消息
                        this.chat.V2NIMMessageService.sendMessage(message, list.conversationId)
                        // Update UI with success
                        } catch (err) {
                        // todo error
                        }
                      }
                    }
                  })
                })
            })
            this.chat.V2NIMLoginService.on('onDisconnected',(error)=> {
                //登录失败
                ElMessage({
                  message: error,
                  type: 'success',
                })
            })
            this.chat.V2NIMLoginService.on('onConnectFailed',(error)=> {
                //登录失败
                ElMessage({
                  message: error,
                  type: 'success',
                })
            })
            this.chat.V2NIMLoginService.login(this.accid,this.toekn,{
              "forceMode": true
            })
          } else {
            //登录失败
            ElMessage({
              message: res.data.msg,
              type: 'error',
            })
          }
        }).catch(err => {
          console.log(err)
        })
      }
    },
    // 接收消息
}
</script>

<style>
#app {
  font-family: Avenir, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
  width: 80%;
  margin: 0 auto;
}

.demo-tabs .el-tabs__content {
  padding: 32px;
  color: #6b778c;
  font-size: 32px;
  font-weight: 600;
}
.el-table{
  display: flex;
  flex-direction: column;
}
.el-table__body-wrapper {
  order: 1;
}
</style>

